package be.pluyckx.batteryprogress.logging;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import be.pluyckx.batteryprogress.application.Constants;
import be.pluyckx.batteryprogress.data.history.BatteryHistory;
import be.pluyckx.batteryprogress.data.history.BatteryLevel;

/**
 * Created by Philip on 28/08/13.
 */
public class BatteryHistoryDump {
  private final File mDirectory;

  public BatteryHistoryDump(File directory) {
    mDirectory = directory;

    if (!mDirectory.exists()) {
      mDirectory.mkdirs();
    } else if (!mDirectory.isDirectory()) {
      throw new IllegalArgumentException("The parameter must be a directory!");
    }
  }

  public void autoDumpFileThreaded(BatteryHistory history, Callback callback) {
    if(Constants.getStoreHistory()) {
      dumpFileThreaded(history, callback);
    }
  }

  public void autoDumpFile(BatteryHistory history, Callback callback) {
    if(Constants.getStoreHistory()) {
      dumpFile(history, callback);
    }
  }

  public void dumpFileThreaded(final BatteryHistory history, final Callback callback) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        dumpFile(history, callback);
      }
    }).start();
  }

  public void dumpFile(BatteryHistory history, Callback callback) {
    if (history.size() == 0) {
      if(callback != null) {
        callback.historyIsEmpty();
      }
      return;
    }

    File path = null;
    File directory = mDirectory;
    String filename = "data-" + Constants.dateTimeFileFormat.format(new Date(System.currentTimeMillis()));
    int index = 0;
    do {
      path = new File(directory, filename + (index == 0 ? "" : Integer.toString(index)) + ".csv");
      index++;
    } while (path.exists());

    try {
      path.createNewFile();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    FileWriter fw = null;
    try {
      fw = new FileWriter(path);
      int i = 0;
      BatteryLevel level = history.get(i);
      writeLevel(fw, level);
      for (i = 1; i < history.size(); i++) {
        fw.write("\n");
        writeLevel(fw, history.get(i));
      }

      if(callback != null) {
        callback.done(true);
      }
    } catch (IOException ex) {
      ex.printStackTrace();

      if(callback != null) {
        callback.done(false);
      }
    } finally {
      if (fw != null) {
        try {
          fw.close();
        } catch (IOException ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  private void writeLevel(FileWriter fw, BatteryLevel level) throws IOException {
    fw.write(Long.toString(level.getDate()));
    fw.write(",");
    fw.write(Integer.toString(level.getBatteryLevel()));
    fw.write(",");
    fw.write(Integer.toString(level.getBatteryScale()));
    fw.write(",");
    fw.write(Float.toString(level.getBatteryLevelPercentage()));
  }

  public static interface Callback {
    public void done(boolean writeSuccess);
    public void historyIsEmpty();
  }
}
